package com.dhecp.project.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.project.business.domain.ProductActive;
import com.dhecp.project.business.domain.ProductBasis;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.hr.domain.ProductSetup;
import com.dhecp.project.hr.service.ProductSetupService;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 精品库 控制层
 * @author: hlf
 * @create: 2023-03-13 10:40
 */
@Controller
@RequestMapping("/business/productsetup")
public class ProductSetupController extends BaseModuleController {

    private String prefix = "business/productsetup";
    private String moduleTable = ModuleTable.PRODUCT_SETUP;

    @Autowired
    private ProductSetupService businessService;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;


    /**
     * @Description: 打开查询列表页
     * @Param: [params]
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: hlf
     * @date: 2022-05-09 11:39
     */
    @RequiresPermissions("business:productsetup:view")
    @GetMapping()
    public ModelAndView view(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        params.put("entity", new ProductSetup()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        params.put("prefix", prefix + "/productsetup"); //页面路径
        params.put("toolBarWidth", 0);
        params.put("checkBox", "yes");
        
    	Object importFields = dictionaryForFieldService.queryFieldFrontImport(params); //精品批量导入字段

        ModelAndView mv = this.openListPage(params);
        mv.addObject("business", "productsetup");
        mv.addObject("importFields",importFields);				//精品批量导入字段模板
        return mv;
    }

    /**
     * @Description: 查询页 列表数据
     * @Param: [params]
     * @return: com.dhecp.common.core.page.TableDataInfo
     * @Author: hlf
     * @date: 2022-05-09 11:41
     */
    @RequiresPermissions("business:productsetup:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        List<ProductSetup> list = new ArrayList<ProductSetup>();
        try {
            list = businessService.queryDataList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }


    /**
     * 导出Listing库
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @Log(title = "精品库导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("business:productsetup:export")
    @GetMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception{
        params.put("moduleTable", moduleTable);
        params.put("entity", new ProductSetup());
        List<ProductSetup> list = businessService.queryDataList(params);
//        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        params.put("getFieldAttributesType", "export");
        List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<ProductSetup> util = new ExportExcelUtils<ProductSetup>(moduleTable,new ProductSetup());
        return util.exportExcel(list, fieldList, "精品库", "export");
    }
    
    /**
     * 导出Excel导入模板
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productsetup:import")
    @Log(title = "精品设置导入模板", businessType = BusinessType.EXPORT)
    @GetMapping("/exporttemplate")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult exportTemplateActive(@RequestParam Map<String, Object> params) throws Exception{
		params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductActive()); 
    	params.put("getFieldAttributesType", "template");

    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<ProductBasis> util = new ExportExcelUtils<ProductBasis>(moduleTable,new ProductBasis());
        return util.exportExcel(null, fieldList, "精品设置导入模板", "template");
    }
    
    /**
     * 导入商品信息至商品库过渡表——数据提交
	 * @param productBasisItems 必要参数：基础商品库对应实体类（ProductBasis）的json格式字符串
     * @return
     * @author DH-Xxj
     */
    @Log(title = "精品设置批量修改数据", businessType = BusinessType.INSERT)
    @RequiresPermissions("business:productsetup:import")
    @PostMapping("/importpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult importSave(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= businessService.importBatch(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}